package com.dao;
/***
*
*/
import com.util.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class OrderDao {
	public OrderDao(){
	
	}
	public int addOrder(HttpServletRequest req){
		String shopid=req.getParameter("shopid")==null?"":req.getParameter("shopid");
		String total=req.getParameter("total")==null?"":req.getParameter("total");
		String address=req.getParameter("address")==null?"":req.getParameter("address");
		String phone=req.getParameter("phone")==null?"":req.getParameter("phone");
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		String reduce=req.getParameter("reduce")==null?"":req.getParameter("reduce");
		String money=req.getParameter("money")==null?"":req.getParameter("money");
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		String orderstatus=req.getParameter("orderstatus")==null?"":req.getParameter("orderstatus");
		String sql ="insert into TBL_ORDER("+
        		"SHOPID"+
        		",TOTAL"+
        		",ADDRESS"+
        		",PHONE"+
        		",NAME"+
        		",REDUCE"+
        		",MONEY"+
        		",CREATETIME"+
        		",ORDERSTATUS"+
        	") values("+
        		"'"+shopid+"'"+
        	        		",'"+total+"'"+
        	        		",'"+address+"'"+
        	        		",'"+phone+"'"+
        	        		",'"+name+"'"+
        	        		",'"+reduce+"'"+
        	        		",'"+money+"'"+
        	        		",'"+createtime+"'"+
        	        		",'"+orderstatus+"'"+
        	")";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public int delOrder(HttpServletRequest req){
         String orderid=req.getParameter("orderid");
		String sql ="delete from TBL_ORDER where 1=1 "+
    		" and orderid ='"+orderid+"'"+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updOrder(HttpServletRequest req){
		String shopid=req.getParameter("shopid")==null?"":req.getParameter("shopid");
		String total=req.getParameter("total")==null?"":req.getParameter("total");
		String address=req.getParameter("address")==null?"":req.getParameter("address");
		String phone=req.getParameter("phone")==null?"":req.getParameter("phone");
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		String reduce=req.getParameter("reduce")==null?"":req.getParameter("reduce");
		String money=req.getParameter("money")==null?"":req.getParameter("money");
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		String orderstatus=req.getParameter("orderstatus")==null?"":req.getParameter("orderstatus");
        String orderid=req.getParameter("orderid");
		String sql ="update TBL_ORDER set "+
        		"SHOPID='"+shopid+"'"+
        		",TOTAL='"+total+"'"+
        		",ADDRESS='"+address+"'"+
        		",PHONE='"+phone+"'"+
        		",NAME='"+name+"'"+
        		",REDUCE='"+reduce+"'"+
        		",MONEY='"+money+"'"+
        		",CREATETIME='"+createtime+"'"+
        		",ORDERSTATUS='"+orderstatus+"'"+
        	" where 1=1 "+
    		" and orderid ='"+orderid+"'"+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findOrderById(HttpServletRequest req){
         String orderid=req.getParameter("orderid");
		String sql ="select * from TBL_ORDER where 1=1 "+
    		" and orderid ='"+orderid+"'"+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	
	public PageModel findOrderByCondition(HttpServletRequest req){
		String shopid=req.getParameter("shopid")==null?"":req.getParameter("shopid");
		req.setAttribute("shopid", shopid);
		String total=req.getParameter("total")==null?"":req.getParameter("total");
		req.setAttribute("total", total);
		String address=req.getParameter("address")==null?"":req.getParameter("address");
		req.setAttribute("address", address);
		String phone=req.getParameter("phone")==null?"":req.getParameter("phone");
		req.setAttribute("phone", phone);
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		req.setAttribute("name", name);
		String reduce=req.getParameter("reduce")==null?"":req.getParameter("reduce");
		req.setAttribute("reduce", reduce);
		String money=req.getParameter("money")==null?"":req.getParameter("money");
		req.setAttribute("money", money);
		String createtime=req.getParameter("createtime")==null?"":req.getParameter("createtime");
		req.setAttribute("createtime", createtime);
		String orderstatus=req.getParameter("orderstatus")==null?"":req.getParameter("orderstatus");
		req.setAttribute("orderstatus", orderstatus);
         String orderid=req.getParameter("orderid");
		String sql ="";
		String where="";
		if(shopid!=null&&!"".equals(shopid)){
    		where+=" and SHOPID like '%"+shopid+"%'";
		}
		if(total!=null&&!"".equals(total)){
    		where+=" and TOTAL like '%"+total+"%'";
		}
		if(address!=null&&!"".equals(address)){
    		where+=" and ADDRESS like '%"+address+"%'";
		}
		if(phone!=null&&!"".equals(phone)){
    		where+=" and PHONE like '%"+phone+"%'";
		}
		if(name!=null&&!"".equals(name)){
    		where+=" and NAME like '%"+name+"%'";
		}
		if(reduce!=null&&!"".equals(reduce)){
    		where+=" and REDUCE like '%"+reduce+"%'";
		}
		if(money!=null&&!"".equals(money)){
    		where+=" and MONEY like '%"+money+"%'";
		}
		if(createtime!=null&&!"".equals(createtime)){
    		where+=" and CREATETIME like '%"+createtime+"%'";
		}
		if(orderstatus!=null&&!"".equals(orderstatus)){
    		where+=" and ORDERSTATUS like '%"+orderstatus+"%'";
		}
    	String countsql ="select count(*) from TBL_ORDER where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select  * from TBL_ORDER where 1=1 "+where+" order by  orderid  desc limit "+(currentPage-1)*pageCount+","+pageCount+"";
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}